//
//  LYEArtistViewController.m
//  artist
//
//  Created by Jay_Apple on 16/2/29.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYEArtistViewController.h"
#import "LYEArtistHeaderViewController.h"
#import "LYEArtistCell.h"
#import "LYEArtistCellHeaderModel.h"
#import "LYEArtisitCellHeaderView.h"
#import "LYVideoModel.h"
#import "LYCollectListModel.h"
#import "LYCityDataDBTool.h"
#import "LYRecommendClassViewController.h"
#import "LYClassWebViewController.h"
#import "LYMineIntrestViewController.h"



@interface LYEArtistViewController ()<UITableViewDataSource, UITableViewDelegate, LYEArtisitCellHeaderViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *models;

@property (assign, nonatomic) NSInteger page;

@property (assign, nonatomic) NSInteger city_id;

@end

@implementation LYEArtistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cityChange:) name:LYCityChangeNotification object:nil];
    
    //初始化tableview
    [self setUpTableView];
    
    [self.tableView.mj_header beginRefreshing];
    
}

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

#pragma mark - 网络请求

/**
 *  请求新数据
 */
- (void)loadNewData{
    
    LYGetSubjectParameterModel *requestModel = [self getRequsetModelWithPage:@"1"];
    
    [LYRequestTool getSubjectListWithParmeterModel:requestModel success:^(NSArray<LYMyClassListModel *> *models) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        self.models = [models mutableCopy];
        [self.tableView reloadData];
        self.page = 1;
        
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_header endRefreshing];
    }];
    
    
}
/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    
    
    LYGetSubjectParameterModel *requestModel = [self getRequsetModelWithPage:page];
    
    [LYRequestTool getSubjectListWithParmeterModel:requestModel success:^(NSArray<LYMyClassListModel *> *models) {
        
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.models addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];
        
        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_footer endRefreshing];
    }];
    
}

#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}

#pragma mark - 通知触发
- (void)cityChange:(NSNotification *)notification{

    self.city_id = [notification.userInfo[@"city_id"] integerValue];
    [self.tableView.mj_header beginRefreshing];

}


#pragma mark - 其他

- (LYGetSubjectParameterModel *)getRequsetModelWithPage:(NSString *)page{

    LYGetSubjectParameterModel *model = [[LYGetSubjectParameterModel alloc] init];
    model.page = page;
    model.per_page = @"10";
    model.token = LYToken;
    if (self.city_id != 0 && self.city_id != -1) {
        model.region = [NSString stringWithFormat:@"%zd",self.city_id];
    }else{
        model.region = @"424";
    }
    
    return model;
    
}

#pragma mark - 初始化
/**
 *  初始化tableview
 */
- (void)setUpTableView{
    
    //header
    LYEArtistHeaderViewController *artistheaderVC = [[LYEArtistHeaderViewController alloc] init];
    [self addChildViewController:artistheaderVC];
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth, LYEArtistHeaderHeight)];
    artistheaderVC.view.My_Size = CGSizeMake(header.My_Width, header.My_Height);
    [header addSubview:artistheaderVC.view];
    self.tableView.tableHeaderView = header;
    
}




#pragma mark - tableViewDataSource
/**
 *  cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYEArtistCell *cell = [LYEArtistCell cellWithTableView:tableView andIndetifier:LYEArtistCellIdentifier];
    cell.model = self.models[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.models.count;
}

#pragma mark - tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return LYEArtistCellHeight;
}



/**
 *  secitonHeader
 */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        LYEArtisitCellHeaderView *header = [LYEArtisitCellHeaderView viewForNib];
        WS(weakSelf);
        
        header.delegate = self;

        
        //配置model
        LYEArtistCellHeaderModel *model = [[LYEArtistCellHeaderModel alloc] init];
        model.IndicatorColor = LYEArtistIndicatorColor;
        model.title = @"推荐课程";
        model.content = @"选择兴趣";
        header.model = model;
        return header;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return LYEArtistCellHeaderHeight;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    LYMyClassListModel *model = self.models[indexPath.row];
    LYClassWebViewController *classWebVC = [LYClassWebViewController classWebVCWithID:model.Id];
    [self.navigationController pushViewController:classWebVC animated:YES];

}

#pragma mark - headerDelegate
- (void)headerContentOnclick:(LYEArtisitCellHeaderView *)header{

    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *model) {
        
        LYMineIntrestViewController *intrestVC = [[LYMineIntrestViewController alloc] init];
        
        [self.navigationController pushViewController:intrestVC animated:YES];
        
        
    }];

    
}

- (void)headerTitleOnclcik:(LYEArtisitCellHeaderView *)header{

    [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *model) {
        
        LYRecommendClassViewController *recommendVC = [[LYRecommendClassViewController alloc] init];
        
        [self.navigationController pushViewController:recommendVC animated:YES];
        
    }];

    
}

@end
